package com.hanlp.utils;


import java.io.IOException;

/**
 * 东方财富API使用示例
 * 演示如何使用各种工具类获取股票和可转债数据
 */
public class EastMoneyApiExample {

    public static void main(String[] args) {
        // 示例1: 获取单只股票实时行情
        System.out.println("=== 示例1: 获取平安银行(000001)实时行情 ===");
        try {
            String stockData = EastMoneyStockUtil.getStockRealTimeData("0.000001");
            System.out.println(stockData);
        } catch (IOException e) {
            System.err.println("获取股票数据失败: " + e.getMessage());
        }

        System.out.println("\n" + "=".repeat(50) + "\n");

        // 示例2: 获取涨幅榜前10名股票
        System.out.println("=== 示例2: 获取涨幅榜前10名股票 ===");
        try {
            String topGainers = EastMoneyApiUtil.getTopGainersStock(10);
            System.out.println(topGainers);
        } catch (IOException e) {
            System.err.println("获取涨幅榜失败: " + e.getMessage());
        }

        System.out.println("\n" + "=".repeat(50) + "\n");

        // 示例3: 获取可转债列表
        System.out.println("=== 示例3: 获取可转债列表前20名 ===");
        try {
            String bondList = EastMoneyBondUtil.getConvertibleBondList(1, 20);
            System.out.println(bondList);
        } catch (IOException e) {
            System.err.println("获取可转债列表失败: " + e.getMessage());
        }

        System.out.println("\n" + "=".repeat(50) + "\n");

        // 示例4: 获取主要指数数据
        System.out.println("=== 示例4: 获取主要指数数据 ===");
        try {
            String indexData = EastMoneyApiUtil.getMajorIndexData();
            System.out.println(indexData);
        } catch (IOException e) {
            System.err.println("获取指数数据失败: " + e.getMessage());
        }

        System.out.println("\n" + "=".repeat(50) + "\n");

        // 示例5: 获取股票K线数据
        System.out.println("=== 示例5: 获取平安银行最近30天日K线数据 ===");
        try {
            String klineData = EastMoneyApiUtil.getStockRecentKLine("000001", 30, EastMoneyBondUtil.KLineType.DAILY);
            System.out.println(klineData);
        } catch (IOException e) {
            System.err.println("获取K线数据失败: " + e.getMessage());
        }

        System.out.println("\n" + "=".repeat(50) + "\n");

        // 示例6: 批量获取多只股票数据
        System.out.println("=== 示例6: 批量获取多只股票数据 ===");
        String[] stockCodes = {"000001", "000002", "600000", "600036"};
        String[] batchData = EastMoneyApiUtil.getMultiStockRealTime(stockCodes, 1000); // 1秒间隔

        for (int i = 0; i < stockCodes.length; i++) {
            System.out.println("股票 " + stockCodes[i] + ": " + batchData[i]);
        }

        System.out.println("\n" + "=".repeat(50) + "\n");

        // 示例7: 获取低溢价率可转债
        System.out.println("=== 示例7: 获取溢价率小于10%的可转债 ===");
        try {
            String lowPremiumBonds = EastMoneyApiUtil.getLowPremiumBonds(10.0, 20);
            System.out.println(lowPremiumBonds);
        } catch (IOException e) {
            System.err.println("获取低溢价率可转债失败: " + e.getMessage());
        }

        System.out.println("\n" + "=".repeat(50) + "\n");

        // 示例8: 使用安全获取方法（带重试）
        System.out.println("=== 示例8: 使用安全获取方法 ===");
        String safeData = EastMoneyApiUtil.safeGetData(
                () -> EastMoneyStockUtil.getStockRealTimeData("0.000001"),
                3,    // 最大重试3次
                2000  // 重试间隔2秒
        );
        System.out.println("安全获取结果: " + safeData);

        System.out.println("\n" + "=".repeat(50) + "\n");

        // 示例9: 获取行业板块排行
        System.out.println("=== 示例9: 获取行业板块排行前10名 ===");
        try {
            String industryRanking = EastMoneyApiUtil.getIndustryRanking(10);
            System.out.println(industryRanking);
        } catch (IOException e) {
            System.err.println("获取行业板块排行失败: " + e.getMessage());
        }

        System.out.println("\n" + "=".repeat(50) + "\n");

        // 示例10: 获取可转债基本信息
        System.out.println("=== 示例10: 获取可转债基本信息 ===");
        try {
            String bondInfo = EastMoneyBondUtil.getAllConvertibleBondInfo(1, 10, "SECURITY_CODE", 1);
            System.out.println(bondInfo);
        } catch (IOException e) {
            System.err.println("获取可转债基本信息失败: " + e.getMessage());
        }

        System.out.println("\n" + "=".repeat(50) + "\n");

    }

    /**
     * 演示如何解析和处理返回的JSON数据
     */
    public static void demonstrateDataProcessing() {
        System.out.println("\n=== 数据处理示例 ===");

        try {
            // 获取股票数据
            String stockData = EastMoneyStockUtil.getStockRealTimeData("0.000001");

            // 检查数据是否成功获取
            if (EastMoneyApiUtil.isResponseSuccess(stockData)) {
                System.out.println("数据获取成功");
                // 这里可以添加JSON解析逻辑
                // 例如使用Jackson或Gson解析JSON
            } else {
                System.out.println("数据获取失败或格式异常");
            }

        } catch (IOException e) {
            System.err.println("网络请求失败: " + e.getMessage());
        }
    }

    /**
     * 演示如何控制请求频率避免被限制
     */
    public static void demonstrateRateLimit() {
        System.out.println("\n=== 请求频率控制示例 ===");

        String[] stockCodes = {"000001", "000002", "600000", "600036", "000858"};

        System.out.println("开始批量获取股票数据，每次请求间隔2秒...");
        long startTime = System.currentTimeMillis();

        String[] results = EastMoneyApiUtil.getMultiStockRealTime(stockCodes, 2000);

        long endTime = System.currentTimeMillis();
        System.out.println("批量获取完成，总耗时: " + (endTime - startTime) + "毫秒");

        // 输出结果
        for (int i = 0; i < stockCodes.length; i++) {
            boolean success = EastMoneyApiUtil.isResponseSuccess(results[i]);
            System.out.println("股票 " + stockCodes[i] + ": " + (success ? "获取成功" : "获取失败"));
        }
    }

    /**
     * 演示不同K线类型的使用
     */
    public static void demonstrateKLineTypes() {
        System.out.println("\n=== K线类型示例 ===");

        String stockCode = "000001";

        try {
            // 日K线
            System.out.println("获取日K线数据...");
            String dailyKLine = EastMoneyApiUtil.getStockRecentKLine(stockCode, 30, EastMoneyBondUtil.KLineType.DAILY);
            System.out.println("日K线数据长度: " + dailyKLine.length());

            // 周K线
            System.out.println("获取周K线数据...");
            String weeklyKLine = EastMoneyApiUtil.getStockRecentKLine(stockCode, 90, EastMoneyBondUtil.KLineType.WEEKLY);
            System.out.println("周K线数据长度: " + weeklyKLine.length());

            // 月K线
            System.out.println("获取月K线数据...");
            String monthlyKLine = EastMoneyApiUtil.getStockRecentKLine(stockCode, 365, EastMoneyBondUtil.KLineType.MONTHLY);
            System.out.println("月K线数据长度: " + monthlyKLine.length());

        } catch (IOException e) {
            System.err.println("获取K线数据失败: " + e.getMessage());
        }
    }

}